package sdk.webview.fmc.com.fmcsdk.util;

import android.app.Notification;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;


import org.json.JSONException;
import org.json.JSONObject;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.activity.H5Activity;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.XLog;

public class SoundUtils {
    private String importance;
    private String messageType;//消息类型
    private SharedPreferences defaultSharedPreferences;
    static Handler handler = new Handler(Looper.getMainLooper());
    private int language;
    private int messageSoundCount;

    private static SoundUtils mInstance;

    private SoundUtils() {
    }

    public static synchronized SoundUtils getInstance() {
        if (mInstance == null) {
            mInstance = new SoundUtils();
        }
        return mInstance;
    }

    public void playSound(final Context context, String extras) {
        defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        language = defaultSharedPreferences.getInt("lang", 0);
        if (!TextUtils.isEmpty(extras)) {
            try {
                JSONObject json = new JSONObject(extras);
                importance = json.optString("importance");
                messageType = json.optString("type");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        //震动提示
        if (defaultSharedPreferences.getBoolean(Constant.VIBRATE, false)) {
//            notification.setDefaults(Notification.DEFAULT_VIBRATE);
        }

        if (!StringUtils.hasCha5miao(defaultSharedPreferences)) {
            return;
        }

        XLog.i("SoundUtils" + messageSoundCount);

        Log.v("shuchang", "SoundUtils--language---->" + language);
        Log.v("shuchang", "SoundUtils--messageSoundCount---->" + messageSoundCount);

        if (defaultSharedPreferences.getBoolean(Constant.SOUND, true)) {
            messageSoundCount = 1 + messageSoundCount;
            if (messageSoundCount == 1) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        defaultSharedPreferences.edit().putLong("lastTime", System.currentTimeMillis()).commit();
                        XLog.i(messageSoundCount + "-----messageSoundCount-----开始执行自定义铃声");
//                    XLog.i(importance+"importance");
                        if (language == 0) {
                            if (messageSoundCount == 1) {
                                if (TextUtils.isEmpty(importance)) {
                                    importance = "-1";
                                }
                                //自定义声音
                                if (importance.equals("1")) {
                                    MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.important)));
                                    if (mp1 != null) {
                                        mp1.start();
                                    }
                                } else {
                                    if (messageType.equalsIgnoreCase(context.getString(R.string.message_to_do))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.newtask)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_grab))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.newgrab)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_notice))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_expire))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.expire)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    }
                                }
                                messageSoundCount = 0;
                            } else if (messageSoundCount > 1) {
                                MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.moretask)));
                                if (mp1 != null) {
                                    mp1.start();
                                }
                                messageSoundCount = -100;
                            }
                        } else if (language == 2) {
                            if (messageSoundCount == 1) {
                                if (TextUtils.isEmpty(importance)) {
                                    importance = "-1";
                                }
                                if (importance.equals("1")) {
                                    MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.important_cht)));
                                    if (mp1 != null) {
                                        mp1.start();
                                    }
                                } else {
//                                XLog.i("~~--"+messageType);
                                    if (messageType.equalsIgnoreCase("待辦")) {
//                                    if (messageType.equalsIgnoreCase(context.getString(R.string.message_to_do))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.newtask_cht)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else if (messageType.equalsIgnoreCase("搶單")) {
//                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_grab))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.newgrab_cht)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_notice))) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
//                                    } else if (messageType.equalsIgnoreCase(context.getString(R.string.message_expire))) {
                                    } else if (messageType.equalsIgnoreCase("工單任務過期")) {
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.expire_cht)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    } else {
//                                    XLog.i("~~--其他");
                                        MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                        if (mp1 != null) {
                                            mp1.start();
                                        }
                                    }

                                }
                                messageSoundCount = 0;
                            } else if (messageSoundCount > 1) {
                                XLog.i(importance + "多个");
                                MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.moretask_cht)));
                                if (mp1 != null) {
                                    mp1.start();
                                }
                                messageSoundCount = -100;
                            }
                        } else {
                            if (messageSoundCount == 1) {
                                if (TextUtils.isEmpty(importance)) {
                                    importance = "-1";
                                }
                                MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                if (mp1 != null) {
                                    mp1.start();
                                }
                                //自定义声音
                                messageSoundCount = 0;
                            } else if (messageSoundCount > 1) {
                                MediaPlayer mp1 = MediaPlayer.create(context, Uri.parse(defaultSharedPreferences.getString(Constant.SOUND_NEW_URL, "android.resource://" + context.getPackageName() + "/" + R.raw.msgnotify)));
                                if (mp1 != null) {
                                    mp1.start();
                                }
                                messageSoundCount = -100;
                            }
                        }
                    }
                }, 1000);
                handler.postDelayed(new Runnable() {
                    public void run() {
                        messageSoundCount = 0;
                    }
                }, 5000);
            }

        }
    }
}
